//
//  SeemeDynamicVC.m
//  SelfDriving
//
//  Created by apple on 16/1/20.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "SeemeDynamicVC.h"
#import "MemberRoamingCell.h"
#import "BusinessService+WD.h"
#import "FriendDetailsMainVC.h"
@interface SeemeDynamicVC ()<BaseCollectionDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet BaseCollection *collection;

@end

@implementation SeemeDynamicVC{
    NSMutableArray *data;
    UILabel *notlabel;//无数据
    UIView *notview;
    int _total;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    data = [[NSMutableArray alloc]init];
    _collection.bdelegate = self;
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(scollTop:)
     name:@"scollTop"
     object:nil];
}
-(void)scollTop:(NSNotification *)not{
    if (data.count!=0) {
        [_collection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                            atScrollPosition:UICollectionViewScrollPositionTop
                                    animated:YES];
    }
}
-(void)initData:(int)index{
    
        NSMutableDictionary *dic = [UserDefaults readUserDefaultObjectValueForKey:@"userLocation"];
        [dic setValue:@"1" forKey:@"type"];
        [dic setObject:[AppDelegate shareDelegate].user.uid forKey:@"fid"];
        [dic setObject:[NSNumber numberWithInt:index] forKey:@"page"];
    WeakSelf
        [[BusinessService shareInstance]getdynamic_guest:dic success:^(id result) {
            if ([[AppDelegate shareDelegate].user.memberlevel isEqualToString:@"0"]) {
                
              UIButton *btn =  [weakSelf.switchView.topScrollView viewWithTag:101];
                
                if (!([(NSMutableArray *)result count] == 0)) {
                    _total = [result[@"total"] intValue];
                }else{
                    _total = 0;
                }
                
                
                
                NSString *title = _total == 0 ? @"动态" : [NSString stringWithFormat:@"动态（%d）",_total];
                
                [btn setTitle:title forState:UIControlStateNormal];
                

                [self notdongtai];
            }else{
            data = [_collection result:result[@"list"] olddata:data index:index];
            
                [_collection reloadData];}
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
    
    
    
}
#pragma  mark 无动态数据时
-(void)notdongtai{
    _collection.hidden = YES;
    notview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, _collection.frame.size.height)];
    [self.view addSubview:notview];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-40, 44, 80, 80)];
    img.image = [UIImage imageNamed:@"skhead"];
    [notview addSubview:img];
    
    
    UILabel *hi = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, SCRE_WIDTH, 30)];
    hi.textAlignment = NSTextAlignmentCenter;
//    hi.text = @"hi,嘿";
    hi.text = [NSString stringWithFormat:@"Hi,%@",[AppDelegate shareDelegate].user.nickname];
    [notview addSubview:hi];
    
    UILabel *fj = [[UILabel alloc]initWithFrame:CGRectMake(0, 160, SCRE_WIDTH, 30)];
    fj.textAlignment = NSTextAlignmentCenter;
    
    if (_total == 0) {
        fj.text = @"没人阅读过你的动态";
    }else{
        fj.text = [NSString stringWithFormat:@"有%d人来看过你",_total];
    }
    
    
    [notview addSubview:fj];
    
    UILabel *sk = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, SCRE_WIDTH, 100)];
    sk.textAlignment = NSTextAlignmentCenter;
    sk.text = @"阅读过动态的人只记录期间会员期间的用户";
    sk.font = [UIFont systemFontOfSize:14];
    sk.textColor = [UIColor grayColor];
    sk.numberOfLines = 0;
    [notview addSubview:sk];
    
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, _collection.frame.size.height-40, SCRE_WIDTH, 40)];
    [btn setTitle:@"开通SVIP，查看阅读动态的人" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor blackColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(tovip:) forControlEvents:UIControlEventTouchUpInside];
    [notview addSubview:btn];
    
}
-(void)tovip:(UIButton *)btn{
    [[NSNotificationCenter defaultCenter ]postNotificationName:@"toVipAction" object:nil];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectio{
    return data.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MemberRoamingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MemberRoamingCell" forIndexPath:indexPath];
    LoginResponseModel *user = [data objectAtIndex:indexPath.row];
    cell.name_label.text = user.nickname;
    cell.name_label.textColor = [UIColor whiteColor];
    cell.vip_img.hidden = YES;
    [ValidateHelper setImage:user.headsmall img:cell.head_img istoux:NO];
    NSString *age = [ValidateHelper timeToAge:user.age];
    NSString *dis = [ValidateHelper DistanceFormat:user.distance];
    NSString *time = [ValidateHelper timeStringWith:[user.viewtime doubleValue] multiLine:NO];
    NSString *sign = [NSString stringWithFormat:@"%@岁 %@ %@",age,dis,time];
    cell.sign_lable.text = sign;
    cell.sign_lable.font = [UIFont systemFontOfSize:10];
//    cell.sign_lable.hidden = YES;

    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCRE_WIDTH-30)/2, (SCRE_WIDTH-30)/2);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LoginResponseModel *model = [data objectAtIndex:indexPath.row];
     
    DTModel *mode = [[DTModel alloc]init];
    mode.uid = model.uid;
    [[NSNotificationCenter defaultCenter ]postNotificationName:@"togerenAction" object:mode];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
